//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "fn_addMobileRespawn.sqf"
#include <btc_macros.h>
TRACE_1("Start",_this);

private ["_vec","_var_name","_side","_str_array","_array"];

if (!isServer) exitWith {};
DEFAULT_PARAM(_vec,0,objNull);
DEFAULT_PARAM(_var_name,1,"");
DEFAULT_PARAM(_side,2,"west");

if (typename _side == typename west) then {
	_side = str(_side);
};
if (typename _side == typename "") then {
	_side = switch(toUpper(_side)) do {
		case "EAST" : { "east" };
		case "OPFOR" : { "east" };
		case "GUER" : { "guer" };
		case "RESISTANCE" : { "guer" };
		case "INDEPENDENT" : { "guer" };
		case "CIV" : { "civ" };
		case "CIVILIAN" : { "civ" };
		default { "west" };
	};
};
TRACE_1("Treated",ARR_3(_vec,_var_name,_side));

if (!isnil "_vec" && {typename _vec == typename objNull} && {!isnull _vec} && {(_vec isKindOf "AllVehicles" || _vec isKindOf "Building")}) then {
	_str_array = format["BTC_vehs_mobile_%1_str",_side];
	_array = missionNamespace getVariable [_str_array,[]];

	if (_var_name == "") then {
		_var_name = format["BTC_Mobile_%1_%2",_side,count _array];
	};
	if (_var_name in _array) then {
		ERROR_1("Already present",_var_name);
	} else {
		_array set [count _array,_var_name];
		missionNamespace setVariable [_str_array,_array];
		publicVariable _str_array;
		
		[_vec,_var_name,_side] spawn BTCR_fnc_vehicleMobileRespawn;
		if (BTC_mobile_teleport == 1) then {
			["BTCR_pveh_vec",[100,_side,_vec,_var_name]] call FUNC(callEvent);
		};
		if (BTC_mobile_heal == 1) then {
			["BTCR_pveh_vec",[101,_side,_vec,_var_name]] call FUNC(callEvent);
		};
		if (BTC_mobile_ammo == 1) then {
			["BTCR_pveh_vec",[102,_side,_vec,_var_name]] call FUNC(callEvent);
		};
		TRACE_1("Processed",ARR_3(_str_array,_var_name,_array));
	};
} else {
	ERROR_1("Invalid Vehicle",_vec);
};